/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller;

import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
import queenb.net.entitybean.N100;
import queenb.net.entitybean.V180;
import queenb.web.crm.controller.V030controller;
import queenb.web.crm.store.CustomerStore;
import queenb.web.crm.store.V030Store;
import queenb.web.store.V180Store;
import queenb.web.util.ComboboxUtil;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;

/**
 *
 * @author Nguyen Hung
 */
public class V180Controller extends QueenbController {

    private int pv180;
    private String vv181;//-- VV181` varchar(32), -- tieu dde
    private String vv182;
    private int fv150;
    private int fn100;
    private int fv050;
    private V180Store v180Store;
    private List<V180> listoftabv180 = new ArrayList<V180>();
    private List<N100> listoftabv180forN100 = new ArrayList<N100>();// lít map tu N100
    private List<SelectItem> comboV030Tour = new ArrayList<SelectItem>();
    public List<N100> listSelected = new ArrayList<N100>();
    private List<N100> listoftabv030forN100 = new ArrayList<N100>();// chọn cho phong oc
    public V180Controller() {
        v180Store = new V180Store();
        getListoftabv180forN100();
        Object listBean = SessionProvider.getSessionMapValue("LISTV180FORTOUR");
        if (listBean == null) {
            SessionProvider.setSessionMapValue("LISTV180FORTOUR", listoftabv180forN100);
        } else {
            try {
                listoftabv180forN100 = (List<N100>) listBean;
                System.out.println("-----------------LIST THEM--------------" + listoftabv180forN100.size());
            } catch (Exception e) {
                listoftabv180forN100 = new ArrayList<N100>();
            }
        }
        Object listBeanpc = SessionProvider.getSessionMapValue("LISTV030ROOM");
        if (listBeanpc == null) {
            SessionProvider.setSessionMapValue("LISTV030ROOM", listoftabv030forN100);
        } else {
            try {
                listoftabv030forN100 = (List<N100>) listBeanpc;
                System.out.println("-----------------LIST THEM----PHONG OC----------" + listoftabv030forN100.size());
            } catch (Exception e) {
                listoftabv030forN100 = new ArrayList<N100>();
            }
        }
        Object listBean1 = SessionProvider.getSessionMapValue("listSelected");
        if (listBean1 == null) {
            SessionProvider.setSessionMapValue("listSelected", listSelected);
        } else {
            try {
                listSelected = (List<N100>) listBean1;
            } catch (Exception e) {
                listSelected = new ArrayList<N100>();
            }
        }
        Object action = getRequest().getParameter("action");
        Object pa000Object = getRequest().getParameter("pa000");
        if (action != null) {
            if (action.equals("deleteV180")) {
                Object Ofv050 = getRequest().getParameter("fv150");
                if (Ofv050 != null) {
                    fv150 = Util.parseInt(Ofv050);
                    SessionProvider.setSessionMapValue("IDELETEV150", fv150);
                }

                Object objFn100 = getRequest().getParameter("fn100");
                if (objFn100 != null) {
                    fn100 = Util.parseInt(objFn100);
                    SessionProvider.setSessionMapValue("IDELETEV180FN100", fn100);
                }
            } else if (action.equals("addAllpersonalforV180")) {
                if (SessionProvider.getSessionMapValue("listSelectedHK") != null) {
                    SessionProvider.removeSessionMapValue("listSelectedHK");
                }

                if (SessionProvider.getSessionMapValue("listSelectedEx") != null) {
                    SessionProvider.removeSessionMapValue("listSelectedEx");
                }
                Object va = getRequest().getParameter("boool");
                if ("true".equals(va.toString())) {
                    listSelected.clear();
                    for (int i = 0; i < listoftabv180forN100.size(); i++) {
                        if (listoftabv180forN100.get(i).getFn100() != 0) {
                            listSelected.add(listoftabv180forN100.get(i));
                        } else {
                            listSelected.add(listoftabv180forN100.get(i));
                        }
                    }
                } else {
                    listSelected.clear();
                }
                System.out.println("---------DANH SACH NGUOI THEO HANG KHONG---------------" + listSelected.size());
                SessionProvider.setSessionMapValue("listSelected", listSelected);
                SessionProvider.setSessionMapValue("LISTV180FORTOUR", listoftabv180forN100);
            } else if (action.equals("addpersonalforV180")) {
                if (SessionProvider.getSessionMapValue("listSelectedHK") != null) {
                    SessionProvider.removeSessionMapValue("listSelectedHK");
                }

                if (SessionProvider.getSessionMapValue("listSelectedEx") != null) {
                    SessionProvider.removeSessionMapValue("listSelectedEx");
                }
                Object f = getRequest().getParameter("pn100");
                Object s = getRequest().getParameter("valueC");
                if ("false".equals(s.toString())) {
                    for (int i = 0; i < listSelected.size(); i++) {
                        if (listSelected.get(i).getFn100() == Integer.parseInt(f.toString())) {
                            listSelected.remove(i);
                            break;
                        }
                    }
                } else {

                    for (int i = 0; i < listoftabv180forN100.size(); i++) {

                        if (listoftabv180forN100.get(i).getFn100() == Integer.parseInt(f.toString())) {
                            if (listSelected.size() > 0) {
                                int flag = 0;
                                for (int j = 0; j < listSelected.size(); j++) {
                                   
                                    if (listSelected.get(j).getFn100() == Integer.parseInt(f.toString())) {
                                     
                                        flag = 1;
                                    }

                                }
                                if (flag ==0) {                               
                                    listSelected.add(listoftabv180forN100.get(i));
                                    break;
                                }
                            } else {
                                listSelected.add(listoftabv180forN100.get(i));
                                break;
                            }


                        }
                    }
                }
                SessionProvider.setSessionMapValue("listSelected", listSelected);
                SessionProvider.setSessionMapValue("LISTV180FORTOUR", listoftabv180forN100);

                System.out.println("---------DANH SACH NGUOI THEO HANG KHONG---------------" + listSelected.size());
            }else if (action.equals("addAllpersonalforV030")) {
                if (SessionProvider.getSessionMapValue("listSelectedHK") != null) {
                    SessionProvider.removeSessionMapValue("listSelectedHK");
                }

                if (SessionProvider.getSessionMapValue("listSelectedEx") != null) {
                    SessionProvider.removeSessionMapValue("listSelectedEx");
                }
                Object va = getRequest().getParameter("boool");
                if ("true".equals(va.toString())) {
                    listSelected.clear();
                    for (int i = 0; i < listoftabv030forN100.size(); i++) {
                        if (listoftabv030forN100.get(i).getPn100() != 0) {
                            listSelected.add(listoftabv030forN100.get(i));
                        } else {
                            listSelected.add(listoftabv030forN100.get(i));
                        }
                    }
                } else {
                    listSelected.clear();
                }
                System.out.println("---------DANH SACH NGUOI PHONG OC---------------" + listSelected.size());
                SessionProvider.setSessionMapValue("listSelected", listSelected);
                SessionProvider.setSessionMapValue("LISTV030ROOM", listoftabv030forN100);
            } else if (action.equals("addpersonalforV030")) {
                if (SessionProvider.getSessionMapValue("listSelectedHK") != null) {
                    SessionProvider.removeSessionMapValue("listSelectedHK");
                }

                if (SessionProvider.getSessionMapValue("listSelectedEx") != null) {
                    SessionProvider.removeSessionMapValue("listSelectedEx");
                }
                Object f = getRequest().getParameter("pn100");
                Object s = getRequest().getParameter("valueC");
                if ("false".equals(s.toString())) {
                    for (int i = 0; i < listSelected.size(); i++) {
                        if (listSelected.get(i).getPn100() == Integer.parseInt(f.toString())) {
                            listSelected.remove(i);
                            break;
                        }
                    }
                } else {

                    for (int i = 0; i < listoftabv030forN100.size(); i++) {
                        if (listoftabv030forN100.get(i).getPn100() == Integer.parseInt(f.toString())) {                        
                                listSelected.add(listoftabv030forN100.get(i));
                                break;
                        }
                    }
                }
                SessionProvider.setSessionMapValue("listSelected", listSelected);
                SessionProvider.setSessionMapValue("LISTV030ROOM", listoftabv030forN100);

                System.out.println("---------DANH SACH NGUOI PHONG OC---------------" + listSelected.size());
            }
        }
    }
    // delete V180

    public void stdv_tools_stornotabv180() {
        try {
            int count = v180Store.stdv_tools_stornotabv180(Util.parseInt(SessionProvider.getSessionMapValue("IDELETEV150")), Util.parseInt(SessionProvider.getSessionMapValue("IDELETEV180FN100")), getLogin().getUserName());
            System.out.println("------COUNT DLETE V180--" + count);
        } catch (Exception e) {
        }
    }

    public void stdv_tools_inserttabv180hide() {
        try {
            if (SessionProvider.getSessionMapValue("IDIDIDFV150") != null) {

                fv150 = Util.parseInt(SessionProvider.getSessionMapValue("IDIDIDFV150"));
            }
            V030controller v030 = new V030controller();
            sendRedirectUrlTab("customer4tour.jsp?status=T&pv050=" + v030.getFV050() + "&action=delSS&tab=upheader" + fv150);
        } catch (Exception e) {
        }
    }

    public void stdv_tools_inserttabv180() {
        //  System.out.println("--------------" + pa000+av001+av002+av003+fn750);
        try {
            V030controller v030 = new V030controller();
            if (SessionProvider.getSessionMapValue("IDIDIDFV150") != null) {

                fv150 = Util.parseInt(SessionProvider.getSessionMapValue("IDIDIDFV150"));
            }
            List<N100> listHK = new ArrayList<N100>();
            if (SessionProvider.getSessionMapValue("listSelectedHK") != null) {
                listHK = (List<N100>) SessionProvider.getSessionMapValue("listSelectedHK");
            }
            int count = 0;
            if (listHK != null) {
                for (int i = 0; i < listHK.size(); i++) {
                    count = v180Store.stdv_tools_inserttabv180(getPv180(), getVv181(), getVv182(), v030.getFV050(), fv150, listHK.get(i).getPn100(), getLogin().getUserName());
                }
            }

            CustomerStore store = new CustomerStore();

            if (count > 0) {
                showGlobalReport(true, getResourceString("success"));
            } else {
                showGlobalReport(true, getResourceString("success"));
            }

            //  int count2 = store.updateColumnN100(null, "V180", "FV050", String.valueOf(v030.getFV050()), "PV180", count, getLogin().getUserName());
            sendRedirectUrlTab("customer4tour.jsp?status=T&pv050=" + v030.getFV050() + "&action=delSS&tab=upheader" + fv150);

        } catch (Exception e) {
        }

    }

    /**
     * @return the pv180
     */
    public int getPv180() {
        return pv180;
    }

    /**
     * @param pv180 the pv180 to set
     */
    public void setPv180(int pv180) {
        this.pv180 = pv180;
    }

    /**
     * @return the vv181
     */
    public String getVv181() {
        return vv181;
    }

    /**
     * @param vv181 the vv181 to set
     */
    public void setVv181(String vv181) {
        this.vv181 = vv181;
    }

    /**
     * @return the vv182
     */
    public String getVv182() {
        return vv182;
    }

    /**
     * @param vv182 the vv182 to set
     */
    public void setVv182(String vv182) {
        this.vv182 = vv182;
    }

    /**
     * @return the fv150
     */
    public int getFv150() {
        return fv150;
    }

    /**
     * @param fv150 the fv150 to set
     */
    public void setFv150(int fv150) {
        this.fv150 = fv150;
    }

    /**
     * @return the fn100
     */
    public int getFn100() {
        return fn100;
    }

    /**
     * @param fn100 the fn100 to set
     */
    public void setFn100(int fn100) {
        this.fn100 = fn100;
    }

    /**
     * @return the listoftabv180
     */
    public List<V180> getListoftabv180() {
        try {
            if (SessionProvider.getSessionMapValue("IDIDIDFV150") != null) {

                fv150 = Util.parseInt(SessionProvider.getSessionMapValue("IDIDIDFV150"));
            }
            V030controller v030 = new V030controller();
            listoftabv180 = v180Store.vcurpks_listoftabv180(v030.getFV050(), fn100, fv150, 0, 50, 0, 0, getLogin().getUserName());
            System.out.println("--------LÍT v180" + listoftabv180.size());
            return listoftabv180;
        } catch (Exception e) {
            return null;
        }


    }

    /**
     * @param listoftabv180 the listoftabv180 to set
     */
    public void setListoftabv180(List<V180> listoftabv180) {
        this.listoftabv180 = listoftabv180;
    }

    /**
     * @return the fv050
     */
    public int getFv050() {
        return fv050;
    }

    /**
     * @param fv050 the fv050 to set
     */
    public void setFv050(int fv050) {
        this.fv050 = fv050;
    }

    /**
     * @return the comboV030Tour
     */
    public List<SelectItem> getComboV030Tour() {
        try {
            ComboboxUtil combo = new ComboboxUtil();
            V030controller v030 = new V030controller();
            return combo.getSelectedV030forTour(v030.getFV050());
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * @param comboV030Tour the comboV030Tour to set
     */
    public void setComboV030Tour(List<SelectItem> comboV030Tour) {
        this.comboV030Tour = comboV030Tour;
    }

    /**
     * @return the listoftabv180forN100
     */
    public List<N100> getListoftabv180forN100() {
        try {
            if (SessionProvider.getSessionMapValue("IDIDIDFV150") != null) {

                fv150 = Util.parseInt(SessionProvider.getSessionMapValue("IDIDIDFV150"));
            }
            V030controller v030 = new V030controller();
            V030Store v030store = new V030Store();
            listoftabv180forN100 = v030store.vcurpks_listoftabv180(v030.getFV050(), fn100, fv150, 0, 50, 0, 0, getLogin().getUserName());
            System.out.println("--------LÍT v180" + listoftabv180forN100.size());
            SessionProvider.setSessionMapValue("LISTV180FORTOUR", listoftabv180forN100);
            return listoftabv180forN100;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param listoftabv180forN100 the listoftabv180forN100 to set
     */
    public void setListoftabv180forN100(List<N100> listoftabv180forN100) {
        this.listoftabv180forN100 = listoftabv180forN100;
    }
}
